<?php 

require_once("form/test.php");

class CharForm extends Form {
	
	public function __construct(){
		parent::__construct(array(
			"name" => new CharField(),
		));
	}
	
}

class CharFieldTest extends Test {
	
	public function run(){
		$form = new CharForm();
		$this->tags($form);
		$this->invalid($form);
		$this->valid($form);
		$this->blank($form);
	}
	
	private function tags($form){
		$this->assertEquals($form->render("name"), "<input type=\"text\" name=\"name\" id=\"name\" value=\"\" />");
		$form->populate(array("name" => "My name"));
		$this->assertEquals($form->render("name"), "<input type=\"text\" name=\"name\" id=\"name\" value=\"My name\" />");
		$form->clean();
	}
	
	private function invalid($form){
		$this->assertEquals(FALSE, $form->isValid(), "Form is not valid");
		$this->assertTrue(in_array("name", $form->errors), "Name contains error");
		$this->assertTrue($form->containsError("name"), "Name contains error");
		$this->assertEmpty($form->getValue("name"));
		$form->clean();
	}
	
	private function valid($form){
		$form->populate(array("name" => "My name is Verta"));
		$this->assertTrue($form->isValid(), "Form valid");
		$this->assertFalse(in_array("name", $form->errors), "Name does NOT contain error cause it's valid");
		$this->assertFalse($form->containsError("name"), "Name does NOT contain error cause it's valid");
		$this->assertNotEmpty($form->getValue("name"));
		$this->assertEquals($form->getValue("name"), "My name is Verta");
		$form->clean();
	}
	
	private function blank($form){
		$form->populate(array("name" => "    "));
		$this->assertEquals(FALSE, $form->isValid(), "Form is not valid");
		$this->assertTrue(in_array("name", $form->errors), "Name contains error");
		$this->assertTrue($form->containsError("name"), "Name contains error");
		$this->assertEmpty($form->getValue("name"));
		$form->clean();
	}
	
}

$test = new CharFieldTest();
$test->run();